<!--
 * @Author: early-autumn
 * @Date: 2020-05-28 11:56:36
 * @LastEditors: early-autumn
 * @LastEditTime: 2020-06-17 15:58:22
-->
<template>
  <el-dialog
    title="修改媒介"
    width="380px"
    :visible="value"
    @close="handleClose"
  >
    <el-form ref="form" :model="form" :rules="rules" inline>
      <el-form-item label="旧媒介" prop="medium">
        <el-cascader
          v-model="data.medium"
          disabled
          :style="{ width: '260px' }"
          :options="channelOptions"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="新媒介" prop="medium">
        <el-cascader
          v-model="form.medium"
          :style="{ width: '260px' }"
          placeholder="请选择新媒介"
          :options="channelOptions"
        ></el-cascader>
      </el-form-item>
    </el-form>

    <div slot="footer">
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" :loading="loading" @click="handleSubmit"
        >确定</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { listChannel } from '@/api/erp/channel'
import { updatePerson } from '@/api/services/person'
import { listToTreeOptions } from '@/utils/utils'

export default {
  name: 'ModifyMedium',
  model: {
    prop: 'value',
    event: 'close',
  },
  props: {
    value: {
      type: Boolean,
      default: false,
    },
    data: {
      type: Object,
      default: () => ({
        id: 0,
        medium: [],
      }),
    },
  },
  data() {
    return {
      loading: false,
      form: {
        medium: [],
      },
      rules: {
        medium: {
          type: 'array',
          required: true,
          message: '请选择新媒介',
          trigger: 'blur',
        },
      },
      channelOptions: [],
    }
  },
  watch: {
    value(newVal) {
      if (newVal) {
        this.init()
      } else {
        this.$refs.form.resetFields()
      }
    },
  },
  mounted() {
    this.getChannelOptions()
  },
  methods: {
    init() {
      this.form.medium = this.data.medium
    },
    /**
     * 获取联级渠道
     */
    async getChannelOptions() {
      const { rows } = await listChannel({
        pageNum: 1,
        pageSize: 10000000,
      })

      const options = listToTreeOptions(rows, ({ pid, id, name }) => ({
        parentId: pid,
        id,
        label: name,
        value: name,
      }))

      options.unshift({
        label: '直接上门',
        value: '直接上门',
        children: [
          {
            label: '直接上门',
            value: '直接上门',
            children: [{ label: '直接上门', value: '直接上门' }],
          },
        ],
      })

      this.channelOptions = options
    },
    handleSubmit() {
      this.$refs.form.validate(async (valid) => {
        try {
          if (!valid) {
            return
          }

          this.loading = true

          const [mediumFirst, mediumSecond, mediumThird] = this.form.medium
          await updatePerson({
            id: this.data.id,
            mediumFirst,
            mediumSecond,
            mediumThird,
          })
          this.msgSuccess('修改成功')
          this.handleClose()
          this.$emit('ok')
        } finally {
          this.loading = false
        }
      })
    },
    handleClose() {
      this.$emit('close', false)
    },
  },
}
</script>

<style></style>
